﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Happy.ExtentionMethods;

namespace Happy.ExceptionHanding
{
    /// <summary>
    /// 友好异常注册处，有些异常是专门作为返回值而设计的，这些异常不需要写入日志，异常信息也是作为消息直接返回给界面。
    /// </summary>
    public static class FriendlyExceptionRegistry
    {
        private static readonly Dictionary<Type, bool> _exceptionTypes = new Dictionary<Type, bool>();

        /// <summary>
        /// 注册异常。
        /// </summary>
        public static void Register<TException>()
            where TException : Exception
        {
            Register(typeof(TException));
        }

        /// <summary>
        /// 注册异常。
        /// </summary>
        public static void Register(Type exceptionType)
        {
            exceptionType.MustNotNull("exceptionType");

            _exceptionTypes[exceptionType] = true;
        }

        public static bool IsFriendly(Type exceptionType)
        {
            exceptionType.MustNotNull("exceptionType");

            return _exceptionTypes.ContainsKey(exceptionType);
        }
    }
}
